package com.comac.print.component.app.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.alibaba.fastjson.JSONObject;
import com.comac.mom.simpleintegration.oss.OSSComponent;
import com.comac.mom.simpleintegration.oss.config.OSSConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author yinhaoye
 * @version 1.0
 * @date 2023/8/15 9:00
 * @description
 */
@Component
public class OSSComponentPlus {

    private static final Logger log = LoggerFactory.getLogger(OSSComponent.class);
    @Autowired
    private OSSConfiguration ossConfig;
    @Value("${oss.pathUrl:}")
    String pathUrl;

    public OSSComponentPlus() {
    }

    public JSONObject upload(String fileName, String dir, InputStream context) throws Exception {
        try {
            OssUtilPlus ossUtil = OssUtilPlus.create(this.ossConfig.getEndpoint(), this.ossConfig.getAccessKeyId(), this.ossConfig.getAccessKeySecret(), this.ossConfig.getBucketName());
            return ossUtil.upload(fileName, dir, context, this.pathUrl);
        } catch (Exception var5) {
            log.error("", var5);
            throw var5;
        }
    }

    public JSONObject uploadFile(String dir, File file) throws Exception {
        try {
            return this.upload(file.getName(), dir, new FileInputStream(file));
        } catch (FileNotFoundException var4) {
            log.error("文件读取异常！", var4);
            throw var4;
        } catch (Exception var5) {
            log.error("文件上传失败！", var5);
            throw var5;
        }
    }

    public JSONObject uploadFile(String ossDir, String ossFileName, File file) throws Exception {
        try {
            OssUtilPlus ossUtil = OssUtilPlus.create(this.ossConfig.getEndpoint(), this.ossConfig.getAccessKeyId(), this.ossConfig.getAccessKeySecret(), this.ossConfig.getBucketName());
            return ossUtil.upload(file.getName(), ossDir, ossFileName, new FileInputStream(file));
        } catch (FileNotFoundException var5) {
            log.error("文件读取异常！", var5);
            throw var5;
        } catch (Exception var6) {
            log.error("文件上传失败！", var6);
            throw var6;
        }
    }
}
